//
//  CViewController.m
//  ZhiBoYuan
//
//  Created by mac on 2018/4/12.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "CViewController.h"
#import "MicroCourseListVC.h"
#import "LiveBroadcastListVC.h"
#import "ShaiXuanViewController.h"
#import "PackageCourseVC.h"


@interface CViewController ()<UIScrollViewDelegate>
{
    MicroCourseListVC *microVC;
    LiveBroadcastListVC *liveVC;
    PackageCourseVC *packageVC;
}

@property (nonatomic ,strong) UIScrollView *myScrollView;

@property (nonatomic ,strong) UISegmentedControl *segment;

//@property(nonatomic,strong)NSMutableArray *shaiXuanArr;


@end

@implementation CViewController


- (void)viewWillAppear:(BOOL)animated{

   [super viewWillAppear:animated];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    [self initWithViews];
    

    self.navigationItem.rightBarButtonItem= [[UIBarButtonItem alloc]initWithTitle:@"筛选" style:(UIBarButtonItemStyleDone) target:self action:@selector(open)];
    
    

}
-(void)open{
    
    NSLog(@"筛选");
    
    ShaiXuanViewController *vc = [[ShaiXuanViewController alloc]init];
    
    
    vc.shaiXuanDic = ^(id dictory) {
        
        NSLog(@"筛选结果= %@",dictory);

        //发送通知页面方法
        
        NSString *str=[NSString string];
        if (self.segment.selectedSegmentIndex == 0)
        {
            str= @"AA";
        } else  if (self.segment.selectedSegmentIndex ==1){
            str= @"AB";
        }else{
            str= @"AC";
        }
        
        NSDictionary *dict = [Helper newShaiXuanFromDictionary:dictory];
        //创建通知
        NSNotification *notification =[NSNotification notificationWithName:str object:nil userInfo:dict];
        //通过通知中心发送通知
        [[NSNotificationCenter defaultCenter] postNotification:notification];
        
        
    };
    
    
//    vc.block = ^(NSMutableArray *arr) {
//        NSLog(@"筛选结果= %@",arr);
//
//
//        self.shaiXuanArr = [NSMutableArray array];
//
//        for (NSString *str in arr) {
//            [self.shaiXuanArr addObject:str];
//        }
//
////        ShaiXuan *s = [[ShaiXuan alloc]initWithDictionary:[Helper shaiXuanFromArr:self.shaiXuanArr]];
//
//        //发送通知页面方法
//
//        NSString *str=[NSString string];
//        if (self.segment.selectedSegmentIndex == 0)
//        {
//            str= @"AA";
//        } else  if (self.segment.selectedSegmentIndex ==1){
//            str= @"AB";
//        }else{
//            str= @"AC";
//        }
//        NSDictionary *dict = [Helper shaiXuanFromArr:self.shaiXuanArr];
//
//        //创建通知
//        NSNotification *notification =[NSNotification notificationWithName:str object:nil userInfo:dict];
//        //通过通知中心发送通知
//        [[NSNotificationCenter defaultCenter] postNotification:notification];
//
//
//    };
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0)
    {
        vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    }
    else
    {
        self.modalPresentationStyle = UIModalPresentationCurrentContext;
    }
    
    vc.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    
   [self.tabBarController presentViewController:vc animated:YES completion:nil];
    
}



- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger n = scrollView.contentOffset.x / scrollView.frame.size.width;
    self.segment.selectedSegmentIndex = n;
}

- (void)change:(UISegmentedControl*)sender{
    
    NSLog(@"改变-%ld",sender.selectedSegmentIndex);
     [self.myScrollView setContentOffset:CGPointMake(sender.selectedSegmentIndex * self.myScrollView.frame.size.width, 0) animated:YES];
}

- (void)initWithViews{
    [Helper setMoRenNav:self.navigationController];
    [Helper setNavColorFrom:self.navigationController color:ZhuTiColor];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    NSArray *array = [NSArray arrayWithObjects:@"微课",@"直播课",@"套餐课", nil];
    self.segment = [[UISegmentedControl alloc]initWithItems:array];
    self.segment.frame = CGRectMake(ScreenWidth/2-150/2, 25, 150, 30);
    self.segment.tintColor = [UIColor whiteColor];
    self.segment.selectedSegmentIndex = 0;
    [self.segment addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = self.segment;
    
    [self.view addSubview:self.myScrollView];

    microVC = [[MicroCourseListVC alloc] init];
    microVC.view.frame = CGRectMake(0, 0, ScreenWidth, Screenheight - NAVIGATION_BAR_HEIGHT- TAB_BAR_HEIGHT);
    [self addChildViewController:microVC];
    [self.myScrollView addSubview:microVC.view];
    
    liveVC = [[LiveBroadcastListVC alloc] init];
    liveVC.view.frame = CGRectMake(ScreenWidth, 0, ScreenWidth, Screenheight - NAVIGATION_BAR_HEIGHT- TAB_BAR_HEIGHT);
    [self addChildViewController:liveVC];
    [self.myScrollView addSubview:liveVC.view];
    
    packageVC = [[PackageCourseVC alloc] init];
    packageVC.view.frame = CGRectMake(ScreenWidth*2, 0, ScreenWidth, Screenheight - NAVIGATION_BAR_HEIGHT- TAB_BAR_HEIGHT);
    [self addChildViewController:packageVC];
    [self.myScrollView addSubview:packageVC.view];
    
}

- (UIScrollView *)myScrollView{
    
    if (!_myScrollView) {
        _myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, ScreenWidth, Screenheight-NAVIGATION_BAR_HEIGHT-TAB_BAR_HEIGHT)];
        _myScrollView.pagingEnabled = YES;
        _myScrollView.bounces = NO;
        _myScrollView.delegate = self;
        _myScrollView.showsHorizontalScrollIndicator = NO;
        _myScrollView.contentSize = CGSizeMake(ScreenWidth*3, Screenheight-NAVIGATION_BAR_HEIGHT-TAB_BAR_HEIGHT);
    }
    return _myScrollView;
}

@end
